﻿using System;
using System.Collections.Generic;
using Assist;

namespace HYRElectric.Calculate
{
	internal class StuFloorRef
	{
		public StuFloorRef(double dVal1, double dVal2, double dVal3)
		{
			this.dCeiling = dVal1;
			this.dWall = dVal2;
			this.dFloor = dVal3;
		}

		public double Cal()
		{
			InterpolationType interpolationType = InterpolationType.eIn;
			double num = 0.0;
			double num2 = 0.0;
			double result = 0.0;
			if (this.FindEqualValue())
			{
				num = FindUseFactor.m_stuUseFactor.dRCR_RI;
				num2 = FindUseFactor.m_stuUseFactor.dRCR_RI;
			}
			else if (!this.FindClosedValue(ref num, ref num2, ref interpolationType))
			{
				return result;
			}
			if (GeoUtil.IsEqual(num, num2))
			{
				this.left = new StuRCR_RI(this.dCeiling, this.dWall, this.dFloor, num);
				result = this.left.Find();
			}
			else
			{
				this.left = new StuRCR_RI(this.dCeiling, this.dWall, this.dFloor, num);
				this.right = new StuRCR_RI(this.dCeiling, this.dWall, this.dFloor, num2);
				double num3 = this.left.Find();
				double num4 = this.right.Find();
				result = num3 + (num4 - num3) * (FindUseFactor.m_stuUseFactor.dRCR_RI - num) / (num2 - num);
			}
			return result;
		}

		private bool FindEqualValue()
		{
			return FindUseFactor.m_arUseFactor.Find((StuUseFactor u) => GeoUtil.IsEqual(u.dRCR_RI, FindUseFactor.m_stuUseFactor.dRCR_RI)) != null;
		}

		private bool FindClosedValue(ref double dLeft, ref double dRight, ref InterpolationType InterType)
		{
			double dRCR_RI = FindUseFactor.m_stuUseFactor.dRCR_RI;
			List<double> list = new List<double>();
			StuFloorRef.RCR_RIComparer comparer = new StuFloorRef.RCR_RIComparer();
			FindUseFactor.m_arUseFactor.Sort(comparer);
			foreach (StuUseFactor stuUseFactor in FindUseFactor.m_arUseFactor)
			{
				list.Add(stuUseFactor.dRCR_RI);
			}
			return FindValue.FindClosedValue(list, dRCR_RI, ref dLeft, ref dRight, ref InterType);
		}

		private double dCeiling;

		private double dWall;

		private double dFloor;

		private StuRCR_RI left;

		private StuRCR_RI right;

		public class RCR_RIComparer : IComparer<StuUseFactor>
		{
			int IComparer<StuUseFactor>.Compare(StuUseFactor first, StuUseFactor second)
			{
				if (first == null && second == null)
				{
					return 0;
				}
				if (first != null && second == null)
				{
					return 1;
				}
				if (first == null && second != null)
				{
					return -1;
				}
				return first.dRCR_RI.CompareTo(second.dRCR_RI);
			}
		}
	}
}
